package univ.bme.accounting.dao;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import univ.bme.accounting.domain.Comment;

@Stateless
public class CommentDAOImpl extends GenericDAOImpl<Comment> implements CommentDAO{

    public CommentDAOImpl() {
    	super(Comment.class);
    }
    
    public void delete(Comment comment) {
        super.delete(comment.getId(), Comment.class);
    }
    
    public List<Comment> findCommentsforTransaction(int transactionId){
    	 CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
         CriteriaQuery<Comment> criteriaQuery = criteriaBuilder.createQuery(Comment.class);
         Root<Comment> entityRoot = criteriaQuery.from(Comment.class);
         criteriaQuery.where(criteriaBuilder.equal(entityRoot.get("transaction"), transactionId));
         criteriaQuery.select(entityRoot);
         List<Comment> c = em.createQuery(criteriaQuery).getResultList();
         return c;
    }
    
	public Long getNumberOfCommentsforTransaction(int transactionId) {
		CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
		CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class);
		Root<Comment> entityRoot = criteriaQuery.from(Comment.class);
		criteriaQuery.select(criteriaBuilder.count(entityRoot.get("id")));
		criteriaQuery.where(criteriaBuilder.equal(entityRoot.get("transaction"), transactionId));
		return em.createQuery(criteriaQuery).getSingleResult();
	}
}